package com.ruoyi.system.interAttr.attr;

import cn.hutool.http.HttpRequest;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.UUIDGenerator;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.V2.gameAttr.mapper.GameAttributeMapper;
import com.ruoyi.system.interAttr.domain.GamesRes;
import com.ruoyi.system.interAttr.domain.InterAttrRes;
import com.ruoyi.system.interAttr.domain.InterServerRes;
import com.ruoyi.system.model.domain.SalePartner;
import com.ruoyi.system.outto.core.MapUntils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

@Slf4j
@Service
public class NEW51YOUXIAttr implements AdapterAttr{
    @Resource
    GameAttributeMapper gameAttributeMapper;
    @Resource
    private RedisCache redisCache;


    private static String SERVICE_NAME = "KJS";


    public static void main(String[] args) {
//         getGames();//获取全部游戏
//        getServer(211); //获取区服
//        getPropety(211); // 获取商品属性
    }

    public List<GamesRes> getGames(SalePartner salePartner) {
        String url = "https://www.51youxihao.com/index.php?g=api&m=product&a=list_games";

        String response = HttpRequest.post(url).execute().body();

        JSONObject resJson = JSONObject.parseObject(response);
        List<JSONObject> jsonObjects = JSONObject.parseArray(resJson.getString("data"), JSONObject.class);

        List<GamesRes> gamesResList = new ArrayList<>();

        for (JSONObject jsonObject : jsonObjects) {
            GamesRes gamesRes = new GamesRes();
            String id = jsonObject.getString("id");
            String title = jsonObject.getString("game_name");
            gamesRes.setId(id);
            gamesRes.setGameName(title);
            gamesResList.add(gamesRes);
        }
        return gamesResList;
    }

    public  List<InterServerRes> getServer(String gameId, SalePartner salePartner) {
        String url = "https://www.51youxihao.com/index.php?g=api&m=product&a=list_games";

        String response = HttpRequest.post(url).execute().body();

        JSONObject jsonObject = JSONObject.parseObject(response);
        List<JSONObject> data = JSONObject.parseArray(jsonObject.getString("data"), JSONObject.class);

        List<InterServerRes> interServerRes = new ArrayList<>();
        for (JSONObject datum : data) {
            String id = datum.getString("id");
            if(id.equals(gameId)){
                JSONArray cate_name = datum.getJSONArray("cate_name");
                for(Object o : cate_name){
                    InterServerRes interServer = new InterServerRes();
                    interServer.setId(o.toString());
                    interServer.setName(o.toString());
                    interServerRes.add(interServer);
                }
            }
        }
        return interServerRes;
    }

    public List<InterAttrRes> getPropety(String gameId, String type, SalePartner salePartner) {
        List<InterAttrRes> interAttrResList = new ArrayList<>();
        if(!type.equals("2")){
            return interAttrResList;
        }
        Map<String, Object> params = new TreeMap<>();
        params.put("platform", "xtdanbao");
        params.put("game_id", gameId);
        String cover = MapUntils.getCover(params);
        cover = cover + "key=wvvbt1rjwvms7o7e319jm7p28jhbqdc3";
        String sign = DigestUtils.md5Hex(cover).toUpperCase();
        params.put("sign", sign);
        String response = HttpUtils.sendPost("https://www.51youxihao.com/index.php?g=api&m=Product&a=list_property", JSONObject.toJSONString(params), "application/json");


        String status = JSONObject.parseObject(response).getString("code");

        if ("200".equals(status)) {
            JSONObject jsonObject = JSONObject.parseObject(response);
            String property = jsonObject.getJSONObject("data").getString("property");
            List<JSONObject> data = JSONArray.parseArray(property, JSONObject.class);
            Map<String, List<JSONObject>> listMap = data.stream().collect(Collectors.groupingBy(s -> s.getString("property")));

            System.out.println(JSONObject.toJSONString(listMap));

            for(Map.Entry<String,List<JSONObject>> entry : listMap.entrySet()){
                String key = entry.getKey();
                InterAttrRes interAttrRes = new InterAttrRes();
                interAttrRes.setId(key);
                interAttrRes.setName(key);
                List<JSONObject> jsonObjects = entry.getValue();
                List<InterAttrRes> interAttrResList1 = new ArrayList<>();
                for (JSONObject jsonObject1 : jsonObjects) {
                    InterAttrRes interAttrRes1 = new InterAttrRes();
                    String skin_id = jsonObject1.getString("skin_id");
                    interAttrRes1.setId(skin_id);
                    String cate_name = jsonObject1.getString("cate_name");
                    interAttrRes1.setName(cate_name);
                    interAttrResList1.add(interAttrRes1);
                }
                interAttrRes.setInterServerRes(interAttrResList1);
                interAttrResList.add(interAttrRes);
            }
        }
        return interAttrResList;
    }

    static Map getCove() {
        String key = "878kjs36bfd324d8";
        String secret = "dd989607c0c5b35422a4f69d306bb0";

        JSONObject params = new JSONObject();
        params.put("key", key);
        params.put("secret", secret);
        String uuid4 = UUIDGenerator.generateUuid6();
        params.put("rand_str", uuid4);
        long time = DateUtils.getNowDate().getTime() / 1000;
        params.put("timestamp", time);
        String oldSign = "key=" + key + ",secret=" + secret + ",rand_str=" + uuid4 + ",timestamp=" + time;
        String sign = DigestUtils.md5Hex(oldSign);
        sign = DigestUtils.sha1Hex(sign + uuid4);
        params.put("sign", sign);
        return params;
    }


}